import request from '@/utils/request'
import type { BaseResponse } from '@/utils/request'
import axios from 'axios'

// 监控点详细信息接口
export interface ChannelInfo {
  CreatedTime: string
  Address: string
  ChannelStatus: number
  ModifiedBy: string
  ChannelId: string
  IsAdd: number
  Name: string
  OwnerId: string
  ModifiedTime: string
  ChannelNo: number
  Permission: number
  PicUrl: string
  CreatedBy: string
  IsShared: string
  ObjectId: string
  DeviceSerial: string
  IsEncrypt: number
  ChannelName: string
  VideoLevel: number
  SensorDeviceMN?: string
  adcode?: string
  OrgId?: string
}

// 获取监控点列表
export const getChannelList = () => {
  return request.get<any, BaseResponse<ChannelInfo[]>>('/h3yun/getChannelList')
}

// 获取主监控点列表
export const getHomeChannelList = (areaCode: string) => {
  return request.get<any, BaseResponse<ChannelInfo[]>>('/h3yun/getHomeChannelList', {params: {areaCode}})
}

/**
 * 查询区域树结构
 * @returns 返回区域树数据
 */
export interface RegionTreeData {
  level: number
  children?: RegionTreeData[]
  center: [number, number]
  label: string
  value: string
}

export interface RegionTreeResponse {
  level: number
  children: RegionTreeData[]
  center: [number, number]
  label: string
  value: string
}

export function getPowerRegionTree() {
  return request.get<any, BaseResponse<RegionTreeResponse>>('/h3yun/getPowerOrgTree')
}

/**
 * 获取下级单位(医院)列表
 * @returns 返回下级单位列表数据
 */
export const queryChildrenOrgList = () => {
  return request.get<any, BaseResponse<any[]>>('/h3yun/queryChildrenOrgList')
}

/**
 * 获取单站配置
 * @returns 返回单站配置
 */
export const queryStationConfig = (params: any) => {
  return request.get<any, BaseResponse<any[]>>('/h3yun/getStationConfig', {params})
}

/**
 * 获取主监控点配置
 * @returns 返回主监控点配置
 */
export const getHomeCameraList = () => {
  return request.get<any, BaseResponse<any[]>>('/h3yun/getHomeCameraList')
}

export const getSensorDeviceList = () => {
  return request.get<any, BaseResponse<any[]>>('/h3yun/getSensorDeviceList')
}

export const getCheckList = () => {
  return request.get<any, BaseResponse<any[]>>('/h3yun/getCheckList')
}

export const addHomeCamera = (data: any) => {
  return request.post<any, BaseResponse<any>>('/h3yun/addHomeCamera', data)
}

export const deleteHomeCamera = (params: any) => {
  return request.get<any, BaseResponse<any>>('/h3yun/deleteHomeCamera', {params})
}

// 更新监控点排序
export const updateHomeCameraOrder = (data: any) => {
  return request.post<any, BaseResponse<any>>('/h3yun/updateHomeCameraOrder', data)
}

export const getBaseCount = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/getBaseCount', {params})
}

export const getCameraOnline = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/getCameraOnline', {params})
}

export const getOrgStatistics = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/getOrgStatistics', {params})
}

export const getOrgMapStatistics = (areaCode: string) => {
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/getOrgMapStatistics', {params})
}

export const getEventCount = (areaCode: string, startTime: string, endTime: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode, startTime, endTime }
  return request.post<any, BaseResponse<any>>('/statistics/getEventCount', params)
}

export const get7DayTrendCount = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/get7DayTrendCount', {params})
}

export const get1YearW00000RtdTrend = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/get1YearW00000RtdTrend', {params})
}

export const getDataUploadTrend = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/getDataUploadTrend', {params})
}

export const get7DayHandlerTrend = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/get7DayHandlerTrend', {params})
}

export const statW00000CountInfo = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/statW00000CountInfo', {params})
}

export const statDisinfectantCountInfo = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/statDisinfectantCountInfo', {params})
}

export const statSludgeCountInfo = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/statSludgeCountInfo', {params})
}

export const statElectricityCountInfo = (areaCode: string) => {
  areaCode = areaCodeHandler(areaCode)
  let params: any = { areaCode }
  return request.get<any, BaseResponse<any>>('/statistics/statElectricityCountInfo', {params})
}

function areaCodeHandler(areaCode: string) {
  if (!areaCode) {
    return ''
  }
  if (areaCode === '100000') {
    return ''
  }
  if (areaCode.endsWith('0000')) {
    areaCode = areaCode.substring(0, 2)
  }
  if (areaCode.endsWith('00')) {
    areaCode = areaCode.substring(0, 4)
  }
  return areaCode
}

// 工单信息接口
export interface WorkOrderInfo {
  SeqNo: string
  Title: string
  OrgId: string
  OrderType: string
  CreatedTime: string
  HandlerStatus: string
  PlanFinishTime: string
  HandlerUser: string
}

// 获取工单列表
export const getWorkOrderList = (areaCode: string) => {
  return request.get<any, BaseResponse<any[]>>('/h3yun/getWorkOrder', {params: {areaCode}})
}

// 获取医院传感器数据
export function getHospitalSensorData(data: {
  cn: string
  areaCode: string
  startTime: string
  endTime: string
}) {
  return request.post<any, BaseResponse<any>>('/statistics/getHospitalSensorData', data)
}

// 导出医院传感器数据
export function exportHospitalSensorData(data: {
  cn: string
  areaCode: string
  startTime: string
  endTime: string
}) {
  return axios.post<Blob>('/api/statistics/exportHospitalSensorData', data, {
    responseType: 'blob'
  })
}

// 通用配置接口
export interface CommonConfig {
  Status: string | null
  CreatedBy: string | null
  CreatedTime: string | null
  code: string
  ObjectId: string
  ModifiedBy: string | null
  title: string
  Name: string | null
  OwnerId: string | null
  ModifiedTime: string | null
  WorkflowInstanceId: string | null
  OwnerDeptId: string | null
  value: string
}

// 获取通用配置
export function getCommonConfig() {
  return request.get<any, BaseResponse<CommonConfig[]>>('/h3yun/getCommonConfig')
}